package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.UIUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;

/**
 * Task status field.
 * 
 * @author dimitri
 */
public class TaskStatusField extends Field {
    private List<Integer> statuses;

    public TaskStatusField() {
        super("taskSearch.status", "სტატუსი");
    }

    public List<Integer> getStatuses() {
        return statuses;
    }

    public void setStatuses(List<Integer> statuses) {
        this.statuses = statuses;
    }
    
    @Override
    public String getType() {
        return "task_status";
    }

    @Override
    public Icon getIcon() {
        return UIUtils.getIcon16x16("email_status.png");
    }

    @Override
    public Condition getCondition() {
        if (statuses == null || statuses.isEmpty()) {
            return null;
        }
        // create where condition
        StringBuilder b = new StringBuilder();
        b.append(" ( ");
        b.append(getSysName());
            b.append(" IN ( ");
        boolean first = true;
        Map<String, Object> subst = new HashMap<String, Object>();
        for (Integer status : statuses) {
            if (!first) {
                b.append(", ");
            }
            String fieldName = getNextField();
            b.append(":");
            b.append(fieldName);
            subst.put(fieldName, status);
            first = false;
        }
        b.append(" ) ) ");
        String where = b.toString();

        // build condition
        Condition condition = new Condition();
        condition.setCondition(where);
        condition.getSubstitutions().putAll(subst);
        return condition;
    }

}